package lostland.gmud.exv2.battle.proc.holder;

import com.orhanobut.logger.Logger;

import java.util.ArrayList;

import lostland.gmud.exv2.Game;
import lostland.gmud.exv2.GameConstants;
import lostland.gmud.exv2.Gmud;
import lostland.gmud.exv2.battle.BattleScreen;
import lostland.gmud.exv2.blgframework.BasicScreen;
import lostland.gmud.exv2.data.NpcModifier;
import lostland.gmud.exv2.ui.SleepScreen;
import lostland.gmud.exv2.ui.TalkingScreen;

/**
 * 赌场战斗任务处理。
 * Created by 12548 on 2015/5/27.
 */
public class DuchangTaskBattle extends BattleHolder {


	int currentBattle = -1;
	int score = 0;

	int badmanCount;


	public DuchangTaskBattle() {
		badmanCount = Gmud.rand.nextInt(7) + 2;
	}

	@Override
	void updato() {

		switch(battle_stat) {

			case BATTLE_PLAYER1_WIN:
				score += 6;
				Logger.d("胜利了一场，目前分数为：%d", score);
				break;
			case BATTLE_PLAYER2_WIN:
				score -= 4;
				Logger.d("失败了一场。");
			case BATTLE_RUNAWAY:
				Logger.d("你逃跑了。");
				score -= 3;
				Gmud.mc.refresh();
				break;
			case BATTLE_NOT_START:
			default:
				Logger.d("战斗尚未开始。");
				break;
		}

		currentBattle++;
		if(currentBattle >= badmanCount) {
			BasicScreen.timeFlow(GameConstants.SECONDS_PER_DAY); //一天过去了

			int goldReward = 0;
			int expReward = 0;

			if(score >= 0) {
				goldReward += 100;
				expReward += 100;
			}

			if(score >= 5) {
				goldReward += 100;
				expReward += 10 * Gmud.mc.getpj();
			}

			if(score > 10) {
				goldReward += 100;
				expReward += 15 * Gmud.mc.getpj();
			}

			if(score > 15) {
				goldReward += 100;
				expReward += 25 * Gmud.mc.getpj();
			}

			int t = score;

			int r3 = t / 30;
			t -= r3 * 30;
			int r2 = t / 15;
			t -= r2 * 15;
			int r1 = t / 5;


			Game.getData().DuchangTaskCooldownDay = Gmud.getGameDay() + 2 + Gmud.rand.nextInt(3);


			Logger.d("赌场任务总分：%d", score);
			String s = "你完成了任务。";
			if(goldReward > 0) {
				s += "你被奖励了：" + goldReward + "金钱，" + expReward + "经验";
				Gmud.mc.earnMoney(goldReward);
				Gmud.mc.setExp(Gmud.mc.exp + expReward);
			}

			if(r1 > 0) {
				s += String.format("，初级符文碎片%d个", r1);
				for(int i = 0; i < r1; i++)
					Gmud.mc.force_give(Gmud.findItem("初级符文碎片").getId());
			}
			if(r2 > 0) {
				s += String.format("，中级符文碎片%d个", r2);
				for(int i = 0; i < r2; i++)
					Gmud.mc.force_give(Gmud.findItem("中级符文碎片").getId());
			}
			if(r3 > 0) {
				s += String.format("，高级符文碎片%d个", r3);
				for(int i = 0; i < r3; i++)
					Gmud.mc.force_give(Gmud.findItem("高级符文碎片").getId());
			}

//			switch(itmRewardLv) {
//				case 1:
//					s += "，初级符文碎片一个";
//					Gmud.mc.give(Gmud.findItem("初级符文碎片").getId());
//					break;
//				case 2:
//					s += "，中级符文碎片一个";
//					Gmud.mc.give(Gmud.findItem("中级符文碎片").getId());
//					break;
//				case 3:
//					s += "，高级符文碎片一个";
//					Gmud.mc.give(Gmud.findItem("高级符文碎片").getId());
//					break;
//			}

			new TalkingScreen(s).pushToGame();
			new SleepScreen(2.5f).pushToGame();
			this.removeFromGame();
			return;
		}

		pass();
	}


	void pass() {

		ArrayList<Integer> b = new ArrayList<>();


		int e1 = Gmud.findNpcId("流氓");
		int e2 = Gmud.findNpcId("盗贼");
		int e3 = Gmud.findNpcId("强盗");
		int e4 = Gmud.findNpcId("恶霸");
		b.add(e1);
		b.add(e2);
		b.add(e3);
		b.add(e4);

		Gmud.getNpc(e1).applyTemplate(NpcModifier.TEMPLATE_DUCHANG_ENEMY_1);
		Gmud.getNpc(e2).applyTemplate(NpcModifier.TEMPLATE_DUCHANG_ENEMY_2);
		Gmud.getNpc(e3).applyTemplate(NpcModifier.TEMPLATE_DUCHANG_ENEMY_3);
		Gmud.getNpc(e4).applyTemplate(NpcModifier.TEMPLATE_DUCHANG_ENEMY_4);

		int index = Gmud.rand.nextInt(b.size());
		int enemy = b.get(index);

		float f = Gmud.rand.nextFloat() * 0.4f + 0.7f;
		Gmud.getNpc(enemy).setDifficulty(f);

		BattleScreen.battle(-1, enemy, null);
		BattleScreen.getInstance().holder = this;
		this.battle_stat = BATTLE_NOT_START;

		switch(index) {
			case 0:
				new TalkingScreen("一个小流氓因为输了很多钱和其他赌徒产生了争执，你便默默地走了过去。").pushToGame();
				break;
			case 1:
				new TalkingScreen("你发现了一个盗贼正在鬼鬼祟祟地偷一个赌徒的荷包。你大步向前一下抓住了他的手腕。").pushToGame();
				break;
			case 2:
				new TalkingScreen("一个身材高大的蒙面强盗突然出现在了门口，你纵身一跃拦到了他的面前。").pushToGame();
				break;
			case 3:
				new TalkingScreen("一个恶名远扬的恶霸来砸场子了，是时候展现自身的实力了。").pushToGame();
				break;
		}

		new SleepScreen(1.5f).pushToGame();
	}
}
